<template>
  <div>
    <el-dialog
      title="退款"
      :visible.sync="visible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="handleCancel"
    >
      <el-row>
        <el-col :span="24">
          <el-descriptions>
            <el-descriptions-item label="支付订单号">
              <el-tag type="primary">
                {{ detailData.payOrderId }}
              </el-tag>
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
        <el-col :span="24">
          <el-descriptions>
            <el-descriptions-item label="支付金额">
              <el-tag type="success">
                {{ detailData.amount / 100 }}
              </el-tag>
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
        <el-col :span="24">
          <el-descriptions>
            <el-descriptions-item label="可退金额">
              <el-tag type="danger">
                {{ nowRefundAmount }}
              </el-tag>
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
      </el-row>

      <el-form :rules="rules" :model="refund" ref="refundInfo">
        <el-form-item label="退款金额" prop="refundAmount">
          <el-input-number v-model="refund.refundAmount" :precision="2" style="width: 100%" />
        </el-form-item>

        <el-form-item label="退款原因" prop="refundReason">
          <el-input v-model="refund.refundReason" type="textarea" />
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { API_URL_PAY_ORDER_LIST, req, payOrderRefund } from '@/api/manage'
export default {
  props: {
    callbackFunc: { type: Function, default: () => () => ({}) }
  },

  data () {
    return {
      recordId: '',
      labelCol: { span: 4 },
      wrapperCol: { span: 16 },
      visible: false,
      confirmLoading: false,
      detailData: {},
      refund: {},
      rules: {
        refundReason: [{ min: 0, max: 256, required: true, trigger: 'blur', message: '请输入退款原因，最长不超过256个字符' }],
        refundAmount: [
          { required: true, message: '请输入金额', trigger: 'blur' },
          {
            validator: (rule, value, callBack) => {
              if (value < 0.01 || value > this.nowRefundAmount) {
                callBack('退款金额不能小于0.01，或者大于可退金额')
              }
              callBack()
            }
          }
        ]
      }
    }
  },
  computed: {
    nowRefundAmount () {
      return (this.detailData.amount - this.detailData.refundAmount) / 100
    }
  },
  methods: {
    show (recordId) {
      if (this.$refs.refundInfo !== undefined) {
        this.$refs.refundInfo.resetFields()
      }
      this.recordId = recordId
      this.visible = true
      this.refund = {}
      const that = this
      req.getById(API_URL_PAY_ORDER_LIST, recordId).then(res => {
        that.detailData = res
      })
    },
    handleOk (e) {
      this.$refs.refundInfo.validate(valid => {
        if (valid) {
          this.confirmLoading = true
          const that = this
          payOrderRefund(that.recordId, that.refund.refundAmount, that.refund.refundReason).then(res => {
            that.visible = false
            that.confirmLoading = false

            if (res.state === 0 || res.state === 3) {
              const refundErrorModal = that.$infoBox.modalError('退款失败', (h) => that.buildModalText(res, h, () => { refundErrorModal.destroy() }))
            } else if (res.state === 1) {
              const refundErrorModal = that.$infoBox.modalWarning('退款中', (h) => that.buildModalText(res, h, () => { refundErrorModal.destroy() }))
              that.callbackFunc()
            } else if (res.state === 2) {
              that.$message.success('退款成功')
              that.callbackFunc()
            } else {
              const refundErrorModal = that.$infoBox.modalWarning('退款状态未知', (h) => that.buildModalText(res, h, () => { refundErrorModal.destroy() }))
            }
          }).catch(() => {
            that.confirmLoading = false
          })
        }
      })
    },
    handleCancel (e) {
      this.visible = false
    },

    buildModalText (res, h, callbackFunc) {
      const toRefundPageBtn = h('a', { on: { click: () => {
            callbackFunc()
            this.$router.push({ name: 'ENT_REFUND_ORDER' })
          } } })
      toRefundPageBtn.text = '退款列表'
      return h('div', [
        h('div', res.errCode ? `错误码：${res.errCode}` : ''),
        h('div', res.errMsg ? `错误信息：${res.errMsg}` : ''),
        h('div', [h('span', '请到'), toRefundPageBtn, h('span', '中查看详细信息')])
      ])
    }
  }
}
</script>

<style scoped lang="less">
</style>


